<?php

/**
 * 工具类
 * File Name：Fun.php
 * File Encoding：UTF-8
 * File New Time：2014-10-21 14:19:06
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class Fun {

    /**
     * 快速文件数据读取和保存 针对简单类型数据 字符串、数组
     * @param string $name 缓存名称
     * @param mixed $value 缓存值
     * @param string $path 缓存路径
     * @param int $expire  有效时间 0为永久
     * @return mixed
     */
    static public function file($name, $value = '', $expire = 300, $path = CLIENT_ROOT) {
        static $_cache = array();
        $filename = $path . $name . '.php';
        if ('' !== $value) {
            if (is_null($value)) {
                // 删除缓存
                if (false !== strpos($name, '*')) {
                    return false; // TODO
                } else {
                    unset($_cache[$name]);
                    return unlink($filename);
                }
            } else {
                // 缓存数据
                $_cache[$name] = $value;
                return self::put($filename, $_cache[$name], $expire);
            }
        }
        // 获取缓存数据
        if (isset($_cache[$name]))
            return $_cache[$name];
        if (is_file($filename)) {
            $content = file_get_contents($filename);
            $expire = (int) substr($content, 8, 12);
            //检查缓存是否过期
            if ($expire != 0 && time() > filemtime($filename) + $expire) {
                unlink($filename);
                return false;
            }
            //数据校验
            $check = substr($content, 20, 32);
            $content = substr($content, 52, -3);
            if ($check != md5($content)) {//校验错误
                return false;
            }
            $_cache[$name] = $value = unserialize($content);
        } else {
            $value = false;
        }
        return $value;
    }

    /**
     * 文件写入
     * @access public
     * @param string $filename  文件名
     * @param string $content  文件内容
     * @param int $expire  有效时间 0为永久
     * @return boolean         
     */
    static public function put($filename, $content, $expire = 0) {
        $data = serialize($content);
        $check = md5($data);
        $data = "<?php\n//" . sprintf('%012d', $expire) . $check . $data . "\n?>";
        $dir = dirname($filename);
        if (!is_dir($dir))
            mkdir($dir, 0755, true);
        if (false === file_put_contents($filename, $data)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获取客户端IP地址
     * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
     * @param boolean $adv 是否进行高级模式获取（有可能被伪装）
     * @return mixed
     */
    static public function get_client_ip($type = 0, $adv = false) {
        $type = $type ? 1 : 0;
        static $ip = NULL;
        if ($ip !== NULL)
            return $ip[$type];
        if ($adv) {
            if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
                $pos = array_search('unknown', $arr);
                if (false !== $pos)
                    unset($arr[$pos]);
                $ip = trim($arr[0]);
            }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                $ip = $_SERVER['HTTP_CLIENT_IP'];
            } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                $ip = $_SERVER['REMOTE_ADDR'];
            }
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        // IP地址合法验证
        $long = sprintf("%u", ip2long($ip));
        $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[$type];
    }

}
